/* 
 * File: loadxmlhandler.h
 * Project: QTimeTracker
 * Author: Steve Sindelar, http://stevesindelar.cz
 * Licence: NewBSD licence.
 */

#include "item.h"
#include <QDomDocument>
#include <QDomElement>
#include <QTextStream>
#include <QFile>
#include <QXmlDefaultHandler>
#include <QXmlSimpleReader>

namespace Core
{

	/**
	 * For loading Items from xml file using Qt SAX.
	 * The result - QList of Item pointers - can be accessed via public field result.
	 * If an error occurres during parsing, error detailed description can be found in public filed errorMessage.
	 */
	class LoadXmlHandler : public QXmlDefaultHandler
	{
	public:
		LoadXmlHandler() {}

		QList<Item*> result;
		QString errorMessage;

		bool startDocument();
		bool fatalError(const QXmlParseException& exception);
		/**
		 * Only end of item element is interesting for this parser.
		 */
		bool endElement(const QString&, const QString&, const QString &name);
		/**
		 * Distributes event to parseSpan or parseTime methods.
		 */
		bool startElement(const QString&, const QString&, const QString &name, const QXmlAttributes &attrs);

	private:
		bool inItem;
		Item *currentItem;

		bool parseItemStart(const QXmlAttributes &attrs);
		bool parseSpan(const QXmlAttributes &attrs);
		/**
		 * Helper method for parsing custom text format representing TimeSpanData class.
		 */
		bool parseTime(QString &attrVal, TimeSpan &span);
	};

}